# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from ui_reactionEdit import Ui_ReactionEditFrame

class ReactionEdit(QtGui.QFrame):
	def __init__(self, controller, reactionClass, relationClass, catalystClass, mode='New', readonly=False):
		QtGui.QFrame.__init__(self)

		self.controller = controller

		self.reactionClass = reactionClass
		self.relationClass = relationClass
		self.catalystClass = catalystClass

		self.reaction = None

        # Set up the user interface from Designer.
		self.ui = Ui_ReactionEditFrame()
		self.ui.setupUi(self)

		self.setMode(mode)

		self.readonly = readonly
		self.setReadonly(readonly)

		self.reactants = []
		self.reactantNames = []

		self.products = []
		self.productNames = []

		self.catalysts = []
		self.catalystNames = []

		self.reference = None

		self.ui.referenceLineEdit.setReadOnly(True)

		self.ui.pressureUnitsComboBox.addItems(["kPa", "bar", "atm"])

		self.ui.solidRadioButton.setChecked(True)

		self.connect(self.ui.addPushButton, QtCore.SIGNAL("clicked()"), self.insertReaction)
		#self.connect(self.ui.updatePushButton, QtCore.SIGNAL("clicked()"), self.updateReaction)

		self.connect(self.ui.searchReactantPushButton, QtCore.SIGNAL("clicked()"), self.searchReactant)
		self.connect(self.ui.searchProductPushButton, QtCore.SIGNAL("clicked()"), self.searchProduct)
		self.connect(self.ui.searchCatalystPushButton, QtCore.SIGNAL("clicked()"), self.searchCatalyst)
		self.connect(self.ui.newCatalystPushButton, QtCore.SIGNAL("clicked()"), self.newCatalyst)
		self.connect(self.ui.searchReferencePushButton, QtCore.SIGNAL("clicked()"), self.searchReference)
		self.connect(self.ui.newReferencePushButton, QtCore.SIGNAL("clicked()"), self.newReference)

		self.connect(self.ui.removeReactantPushButton, QtCore.SIGNAL("clicked()"), self.removeReactant)
		self.connect(self.ui.removeProductPushButton, QtCore.SIGNAL("clicked()"), self.removeProduct)
		self.connect(self.ui.removeCatalystPushButton, QtCore.SIGNAL("clicked()"), self.removeCatalyst)

	def clear(self):
		self.products = []
		self.productNames = []

		self.reactants = []
		self.reactantNames = []

		self.catalysts = []
		self.catalystNames = []

		self.ui.reactantList.clear()
		self.ui.productList.clear()
		self.ui.catalystList.clear()

		self.ui.referenceLineEdit.clear()
		self.ui.temperatureSpinBox.setValue(22)
		self.ui.pressureSpinBox.setValue(101.325)
		self.ui.pressureUnitsComboBox.setCurrentIndex(0)
		self.ui.timeSpinBox.setValue(0)
		self.ui.pHSpinBox.setValue(7)
		self.ui.solventLineEdit.clear()
		self.ui.solidRadioButton.setChecked(True)
		self.ui.liquidRadioButton.setChecked(False)
		self.ui.gasRadioButton.setChecked(False)
		self.ui.otherLineEdit.clear()

		self.ui.temperatureUnknownCheckBox.setChecked(False)
		self.ui.pressureUnknownCheckBox.setChecked(False)
		self.ui.timeUnknownCheckBox.setChecked(False)
		self.ui.pHUnknownCheckBox.setChecked(False)
		self.ui.solventUnknownCheckBox.setChecked(False)
		self.ui.stateUnknownCheckBox.setChecked(False)
		self.ui.otherUnknownCheckBox.setChecked(False)

	def insertReaction(self):
		self.reaction = self.reactionClass()
		self.reaction.insert()

		self.updateReaction()

	def updateReaction(self):
		if not self.ui.temperatureUnknownCheckBox.isChecked():
			self.reaction.temperature = self.ui.temperatureSpinBox.value()
		if not self.ui.pressureUnknownCheckBox.isChecked():
			self.reaction.pressure = self.ui.pressureSpinBox.value()
		if not self.ui.timeUnknownCheckBox.isChecked():
			self.reaction.time = self.ui.timeSpinBox.value()
		if not self.ui.pHUnknownCheckBox.isChecked():
			self.reaction.pH = self.ui.pHSpinBox.value()
		if not self.ui.solventUnknownCheckBox.isChecked():
			self.reaction.solvent = self.ui.solventLineEdit.text()
		if not self.ui.stateUnknownCheckBox.isChecked():
			if self.ui.solidRadioButton.isChecked():
				self.reaction.state = "Solid"
			if self.ui.liquidRadioButton.isChecked():
				self.reaction.state = "Liquid"
			if self.ui.gasRadioButton.isChecked():
				self.reaction.state = "Gas"
		if not self.ui.otherUnknownCheckBox.isChecked():
			self.reaction.other = self.ui.otherLineEdit.text()

		for product in self.products:
			for reactant in self.reactants:
				#TODO check for ones that should be deleted
				relation = self.relationClass()
				relation.reactant = reactant
				relation.product = product
				relation.reaction = self.reaction
				relation.insert()

		for catalyst in self.catalysts:
			catalyst.reaction = self.reaction

		if not self.ui.referenceUnknownCheckBox.isChecked():
			if self.reference:
				self.reaction.reference = self.reference

	def search(self):
		self.reaction = self.reactionClass()
		self.reaction.reactants = self.reactants

	def setCurrentReaction(self, relation):
		self.relation = relation
		relations = self.reactant.relations
		self.reactants = [r.reactant for r in relations]
		self.products = [r.product for r in relations]
		self.catalysts = self.relation.catalysts

		self.updateFields()


	def updateFields(self):
		self.ui.reactantList.addItems([i.name for i in reactants])
		self.ui.productList.addItems([i.name for i in products])
		self.ui.catalystList.addItems([i.name for i in catalysts])

		if self.reaction.reference:
			self.ui.referenceLineEdit.setText(self.reaction.reference.name)
		if self.reaction.temperature:
			self.ui.temperatureSpinBox.setValue(self.reaction.temperature)
		else:
			self.ui.temperatureUnknownCheckBox.setChecked(True)
		if self.reaction.presure:
			self.ui.pressureSpinBox.setValue(self.reaction.pressure)
			self.ui.pressureUnitsComboBox.setCurrentIndex(0)
		else:
			self.ui.pressureUnknownCheckBox.setChecked(True)
		if self.reaction.time:
			self.ui.timeSpinBox.setValue(self.reaction.time)
		else:
			self.ui.timeUnknownCheckBox.setChecked(True)
		if self.reaction.pH:
			self.ui.pHSpinBox.setValue(self.ui.pH)
		else:
			self.ui.pHUnknownCheckBox(True)
		if self.reaction.solvent:
			self.ui.solventLineEdit.setText(self.solvent)
		else:
			self.ui.solventUnknownCheckBox.setChecked(True)
		if self.reaction.state:
			if self.reaction.state == "Solid":
				self.ui.solidRadioButton.setChecked(True)
			elif self.reaction.state == "Liquid":
				self.ui.liquidRadioButton.setChecked(True)
			elif self.reaction.state == "Gas":
				self.ui.gasRadioButton.setChecked(True)
		else:
			self.stateUnknownCheckBox.setChecked(True)
		if self.reaction.other:
			self.ui.otherLineEdit.setText(self.reaction.other)
		else:
			self.ui.otherUnknownCheckBox.setChecked(True)

	def setReadonly(self, readonly):
		self.readonly = readonly

		self.ui.temperatureSpinBox.setReadOnly(readonly)
		self.ui.pressureSpinBox.setReadOnly(readonly)
		self.ui.timeSpinBox.setReadOnly(readonly)
		self.ui.pHSpinBox.setReadOnly(readonly)
		self.ui.solventLineEdit.setReadOnly(readonly)
		#self.ui.solidRadioButton.setReadOnly(readonly)
		#self.ui.liquidRadioButton.setReadOnly(readonly)
		#self.ui.gasRadioButton.setReadOnly(readonly)
		self.ui.otherLineEdit.setReadOnly(readonly)

		#self.ui.temperatureUnknownCheckBox.setReadOnly(readonly)
		#self.ui.pressureUnknownCheckBox.setReadOnly(readonly)
		#self.ui.timeUnknownCheckBox.setReadOnly(readonly)
		#self.ui.pHUnknownCheckBox.setReadOnly(readonly)
		#self.ui.solventUnknownCheckBox.setReadOnly(readonly)
		#self.ui.stateUnknownCheckBox.setReadOnly(readonly)
		#self.ui.otherUnknownCheckBox.setReadOnly(readonly)

	def setMode(self, mode):
		self.mode = mode
		if self.mode == "New":
			self.ui.addPushButton.show()
			self.ui.updatePushButton.hide()
		elif self.mode == "Update":
			self.ui.addPushButton.hide()
			self.ui.updatePushButton.show()

	def searchCatalyst(self):
		self.controller.catalystDialog("Search")

	def newCatalyst(self):
		self.controller.catalystDialog("New")

	def searchProduct(self):
		self.searchCompound("Product")

	def searchReactant(self):
		self.searchCompound("Reactant")

	def searchCompound(self, mode):
		self.controller.searchCompoundDialog(mode)

	def addToList(self, obj, where):
		if where == "Reactant":
			self.reactants.append(obj)
			self.ui.reactantList.addItem(obj.name)
		elif where == "Product":
			self.products.append(obj)
			self.ui.productList.addItem(obj.name)
		elif where == "Catalyst":
			self.catalysts.append(obj)
			self.ui.catalystList.addItem(obj.name)
		elif where == "Reference":
			self.reference = obj
			self.ui.referenceLineEdit.setText(obj.name)

	def searchReference(self):
		self.controller.referenceDialog("Search")

	def newReference(self):
		self.controller.referenceDialog("New")

	def removeReactant(self):
		row = self.ui.reactantList.currentRow()
		del self.reactants[row]
		self.ui.reactantList.takeItem(row)

	def removeProduct(self):
		row = self.ui.productList.currentRow()
		del self.products[row]
		self.ui.productList.takeItem(row)

	def removeCatalyst(self):
		row = self.ui.catalystList.currentRow()
		del self.catalysts[row]
		self.ui.catalystList.takeItem(row)
